package com.zhang.set;

import com.zhang.二叉树.BinaryTree;
import com.zhang.二叉树.RBTree;

import java.util.Comparator;

/**
 * @author 张吉鑫
 * @Description 红黑树实现TreeSet集合
 * @date 2021/3/23 17:46
 */
public class TreeSet<E> implements Set<E>{
    RBTree<E> tree;

    public TreeSet() {
        this(null);
    }

    public TreeSet(Comparator<E> comparator){
        this.tree = new RBTree<E>(comparator);
    }

    @Override
    public int size() {
        return tree.size();
    }

    @Override
    public boolean isEmpty() {
        return tree.isEmpty();
    }

    @Override
    public void clear() {
        tree.clear();
    }

    @Override
    public boolean contains(E element) {
        return tree.contains(element);
    }

    @Override
    public void add(E element) {
        tree.add(element);
    }

    @Override
    public void remove(E element) {
        tree.remover(element);
    }

    @Override
    public void traversal(Visitor<E> visitor) {
        if (visitor == null){
            return;
        }
        tree.inorder(new BinaryTree.Visitor<E>() {
            @Override
            public boolean visitor(E element) {
                return visitor.visit(element);
            }
        });
    }
}
